// pages/sales/boss_ai/add_staff.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
var app = getApp();
Page(Object.assign({}, baseHandle,{

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staffName:"",
    phone:"",
    post:"",
    identify:"",
    name:"",
    isLoading:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ctx = this;
    wx.hideShareMenu();
    app.getUserInfo(ctx, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');

    });
  },
  inputStr(e){
    console.log(e)
    var opname = e.currentTarget.dataset.opname;
    var val = e.detail.value;
    var ctx = this;
    ctx.setData({
      [opname]: val
    });
  },
  //
  addByEmpPrivilege(){
    var ctx = this;

    if (ctx.data.isLoading){
      return;
    }
    if (!ctx.data.staffName){
      wx.showToast({
        title: '请输入员工姓名',
        icon:"none"
      });
      return;
    }
    if (11 != ctx.data.phone.length){
      wx.showToast({
        title: '请输入11位联系电话',
        icon:"none"
      });
      return;
    }
    if (!ctx.data.post){
      wx.showToast({
        title: '请输入员工职位',
        icon:"none"
      });
      return;
    }
    if (!ctx.data.identify){
      wx.showToast({
        title: '请选择部门',
        icon:"none"
      });
      return;
    }
    wx.showLoading({
      title: '提交中',
    });
    ctx.setData({
      isLoading: true
    });
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/addByEmpPrivilege",
      data: {
        cusmallToken: cusmallToken,
        name: ctx.data.staffName,
        phone: ctx.data.phone,
        post: ctx.data.post,
        department: ctx.data.identify
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        wx.hideLoading()
        if (data && 0 === data.ret) {
          wx.showToast({
            title: "添加成功",
            icon: "none",
            success(){
              
            }
          });
          setTimeout(function(){
            let pages = getCurrentPages();//当前页面
            let prevPage = pages[pages.length - 2];//上一页面
            //直接给上移页面赋值
            prevPage.setData({
              reLoadStaffList:true
            });
            wx.navigateBack({
              delta: 1
            });
          },1000)
        } else {
          wx.showToast({
            title: data.msg,
            icon: "none"
          });
        }
      },
      fail() {

      },
      complete() {
        ctx.setData({
          isLoading: false
        });
      }
    });
  },
  selDepart(){
    wx.navigateTo({
      url: './selDepartment?saveKey=identify,name&isShowAll=n',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))